package ciyuan.Item.SkillBook;

import ciyuan.Item.ItemGroups;
import ciyuan.Item.TypeInterface.SkillBookType;
import ciyuan.Player.Skill.SkillUtils;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.item.ArmorStandEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.potion.EffectInstance;
import net.minecraft.potion.Effects;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.server.ServerWorld;
import net.minecraftforge.event.entity.living.LivingDamageEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber
public class ForestPower extends SkillBookType {

    private static final String CastTime = ",ForestPower";

    public ForestPower() {
        super(new Properties().tab(ItemGroups.SkillBook));
    }

    @Override
    public void keyDown(PlayerEntity playerEntity) {
        SkillUtils.setSkillColdInterval(playerEntity, this, 25 * 1000);

        playerEntity.addEffect(new EffectInstance(Effects.MOVEMENT_SPEED, 100, 3));
        SkillUtils.setSaveData(playerEntity, CastTime, System.currentTimeMillis());
        SkillUtils.THREAD_POOL_EXECUTOR.execute(new Runnable() {
            @Override
            public void run() {
                SkillUtils.sleep(5000);
                for(LivingEntity livingentity : playerEntity.level.getEntitiesOfClass(LivingEntity.class, new AxisAlignedBB(
                        playerEntity.getX()+5,playerEntity.getY()+3,playerEntity.getZ()+5,playerEntity.getX()-5,playerEntity.getY()-1,playerEntity.getZ()-5
                ))) {
                    if (livingentity != playerEntity && !playerEntity.isAlliedTo(livingentity) && (!(livingentity instanceof ArmorStandEntity) || !((ArmorStandEntity)livingentity).isMarker()) ) {
                        if (playerEntity.level instanceof ServerWorld) {
                            livingentity.addEffect(new EffectInstance(Effects.MOVEMENT_SLOWDOWN,60,9));
                        }
                    }
                }
            }
        });

        SkillUtils.saveSkillCastTime(playerEntity,this);
    }

    @SubscribeEvent
    public static void onDamage(LivingDamageEvent event) {
        if (event.getEntityLiving() instanceof ServerPlayerEntity) {
            ServerPlayerEntity serverPlayerEntity = (ServerPlayerEntity) event.getEntityLiving();
            if (System.currentTimeMillis() -  SkillUtils.getSaveData(serverPlayerEntity,CastTime)<5*1000) {
                event.setAmount(0);
            }
        }
    }

    @Override
    public String getSkillCastName() {
        return "ForestPower";
    }

    @Override
    public ITextComponent getSkillDescription() {
        return new TranslationTextComponent("ciyuan.skill.desc.25");
    }

    @Override
    public ITextComponent getSkillName() {
        return new TranslationTextComponent("ciyuan.skill.name.25");
    }
}
